#include "StdAfx.h"
#include "MyBitmap.h"
#include "Resource.h"
MyBitmap::MyBitmap(void)
{
}

MyBitmap::~MyBitmap(void)
{
}


MyBitmap::MyBitmap(LPCTSTR str)
{
	cb = new CBitmap();
	HBITMAP g_hbmbg= LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(str));
	cb->Attach(g_hbmbg);
}

//**----------------------------------------------------------

//** STEP 1: Load the bitmap into a CBitmap Object

//**----------------------------------------------------------

void MyBitmap::DrawTransparent(CDC * pDC, int x, int y, COLORREF crColour)
{
	try
	{

	COLORREF crOldBack = pDC->SetBkColor(RGB(255,255,255));
	COLORREF crOldText = pDC->SetTextColor(RGB(0,0,0));
	CDC dcImage, dcTrans,memDC;
	//CClientDC dc=new CClientDC();
	// Create two memory dcs for the image and the mask7
	//memDC.CreateCompatibleDC(NULL);
	dcImage.CreateCompatibleDC(pDC);
	dcTrans.CreateCompatibleDC(pDC);
	// Select the image into the appropriate dc

	CBitmap* pOldBitmapImage = dcImage.SelectObject(cb);

	// Create the mask bitmap
	CBitmap bitmapTrans;
	BITMAP bm;
	cb->GetBitmap(&bm);
	int nWidth = bm.bmWidth;
	int nHeight = bm.bmHeight;
	bitmapTrans.CreateBitmap(nWidth, nHeight, 1, 1, NULL);

	// Select the mask bitmap into the appropriate dc
	CBitmap* pOldBitmapTrans = dcTrans.SelectObject(&bitmapTrans);

	// Build mask based on transparent colour
	dcImage.SetBkColor(crColour);
	dcTrans.BitBlt(0, 0, nWidth, nHeight, &dcImage, 0, 0, SRCCOPY);

	// Do the work - True Mask method - cool if not actual display
	pDC->BitBlt(x, y, nWidth, nHeight, &dcImage, 0, 0, SRCINVERT);
	pDC->BitBlt(x, y, nWidth, nHeight, &dcTrans, 0, 0, SRCAND);
	pDC->BitBlt(x, y, nWidth, nHeight, &dcImage, 0, 0, SRCINVERT);
	
	// Restore settings
	dcImage.SelectObject(pOldBitmapImage);
	dcTrans.SelectObject(pOldBitmapTrans);
	pDC->SetBkColor(crOldBack);
	pDC->SetTextColor(crOldText);
	//DeleteDC(&memDC);

	}
	
	catch(...)
	{
	}
	

}



void MyBitmap::DrawTransparent(CDC * pDC, int x, int y,int x1, int y1,int width,int height, COLORREF crColour)
{

	try
	{
	COLORREF crOldBack = pDC->SetBkColor(RGB(255,255,255));
	COLORREF crOldText = pDC->SetTextColor(RGB(0,0,0));
	CDC dcImage, dcTrans;

	// Create two memory dcs for the image and the mask
	dcImage.CreateCompatibleDC(pDC);
	dcTrans.CreateCompatibleDC(pDC);

	// Select the image into the appropriate dc
	CBitmap* pOldBitmapImage = dcImage.SelectObject(cb);

	// Create the mask bitmap
	CBitmap bitmapTrans;
	BITMAP bm;
	cb->GetBitmap(&bm);
	int nWidth = width;
	int nHeight = height;
	bitmapTrans.CreateBitmap(nWidth, nHeight, 1, 1, NULL);

	// Select the mask bitmap into the appropriate dc
	CBitmap* pOldBitmapTrans = dcTrans.SelectObject(&bitmapTrans);
	pDC->SelectObject(&bm);
	// Build mask based on transparent colour
	dcImage.SetBkColor(crColour);
	dcTrans.BitBlt(x1,y1 , nWidth, nHeight, &dcImage, x1, y1, SRCCOPY);
	

	// Do the work - True Mask method - cool if not actual display
	pDC->BitBlt(x, y,nWidth, nHeight, &dcImage, x1, y1, SRCINVERT);
	pDC->BitBlt(x, y, nWidth, nHeight, &dcTrans, x1, y1, SRCAND);
	pDC->BitBlt(x, y, nWidth, nHeight, &dcImage, x1, y1, SRCINVERT);

	// Restore settings
	dcImage.SelectObject(pOldBitmapImage);
	dcTrans.SelectObject(pOldBitmapTrans);
	pDC->SetBkColor(crOldBack);
	pDC->SetTextColor(crOldText);
	}
	catch(...)
	{
	}

}
void MyBitmap::DrawBG(CDC * pDC,int x,int y)
{
	CDC memDc,dcbuff;
	CRect rect;
	rect.left=0;
	rect.top=600;
	rect.right=800;
	rect.bottom=0;
	memDc.CreateCompatibleDC(0);
	dcbuff.CreateCompatibleDC(pDC);
	dcbuff.FillRect(rect,WHITE_BRUSH);
	BITMAP bm;
	cb->GetBitmap(&bm);
	int nWidth = bm.bmWidth;
	int nHeight = bm.bmHeight;
	CreateBitmapMask(RGB(255,0,255));
	memDc.SelectObject(cb);
	//dcbuff->
	pDC->BitBlt(x, y, nWidth, nHeight,&memDc,0,0,SRCCOPY);
}
void MyBitmap::CreateBitmapMask( COLORREF crTransparent)
{
	CDC hdcMem, hdcMem2;
	CBitmap *hbmMask=new CBitmap();
	BITMAP bm;

	cb->GetBitmap(&bm);

	hbmMask->CreateBitmap(bm.bmWidth, bm.bmHeight, 1, 1, NULL);

	hdcMem.CreateCompatibleDC(0);
	hdcMem2.CreateCompatibleDC(0);

	hdcMem.SelectObject(cb);
	hdcMem2.SelectObject(hbmMask);

	SetBkColor(hdcMem, crTransparent);

	BitBlt(hdcMem2, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);

	BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem2, 0, 0, SRCINVERT);

	DeleteDC(hdcMem);
	DeleteDC(hdcMem2);
	cbmask=hbmMask;

}
